package com.ztgf.pay.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.CustomizeUtil;
import com.ztgf.common.utils.ExcelExport2;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.pay.models.bo.PayZjBalanceBO;
import com.ztgf.pay.models.bo.PayZjBalanceLogBO;
import com.ztgf.pay.models.dto.PayFundDetailsDTO;
import com.ztgf.pay.models.vo.PayBalanceExpotVO;
import com.ztgf.pay.models.vo.PayZjBalanceLogVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import com.ztgf.pay.models.service.IPayUserService;
import com.ztgf.pay.models.bo.PayUserSaveBO;
import com.ztgf.pay.models.bo.PayUserBO;
import com.ztgf.pay.models.dto.PayUserDTO;

import javax.servlet.http.HttpServletResponse;

/**
 * 用户信息表控制器
 *
 * @author mxli
 * @Date 2020-07-30 12:13:56
 */

@RestController
@RequestMapping("/cms/payUser")
@Slf4j
@Api(value = "PayUserController",tags = "用户信息表接口")
public class PayUserController {

    @DubboReference
    private IPayUserService payUserService;
    @Value("${ztgf-income-user-id}")
    private Long ztgfIncomeUserId;//中铁工服收益账号

    @PostMapping("/updateStatus")
    @ApiOperation(value = "修改状态",notes = "修改状态")
     @ApiImplicitParams({
             @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
             ,@ApiImplicitParam(name = "status",value = "状态 0创建中、1进件中、2成功、3失败",required = true,paramType = "query", dataType = "Integer")
     })
     public Result updateStatus(Long id, Integer status){
         PayUserDTO dto = new PayUserDTO();
         dto.setId(id);
         dto.setStatus(status);
        boolean flag = payUserService.updateById(dto);
         if(flag){
             return new Result<>();
         } else {
             return new Result<>(CodeEnum.FAIL_100008);
         }
     }

    @ApiOperation(value = "删除用户信息表",notes = "删除用户信息表")
    @ApiImplicitParam(name = "id",value = "用户信息表id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = payUserService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除用户信息表",notes = "批量删除用户信息表")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        payUserService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<PayUserDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        PayUserDTO dto = payUserService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "用户信息表列表",notes = "用户信息表列表")
    @PostMapping("/queryPageList")
    public Result<Page<PayUserDTO>> queryPageList(PayUserBO bo){
        Wrapper<PayUserDTO> wrapper = new EntityWrapper<>();

        if (bo.getStatus() != null){
            wrapper.eq("status", bo.getStatus());
        }

        if(bo.getUserId() != null){
            wrapper.eq("user_id", bo.getUserId());
        }
        if(bo.getAccountType() != null){
            wrapper.eq("account_type", bo.getAccountType());
        }
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<PayUserDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = payUserService.selectPage(page, wrapper);

        return new Result<>(page);
    }
    @PostMapping("/queryPageZjBalanceLog")
    @ApiOperation(value = "admin获取中金当天交易流水",notes = "admin获取中金当天交易流水")
    public Result<Page<PayZjBalanceLogVO>> queryPageZjBalanceLog(PayZjBalanceLogBO bo){
        bo.setUserId(ztgfIncomeUserId);
        return payUserService.queryPageZjBalanceLog(bo);
    }

    @PostMapping("/queryPageZjHistoryBalanceLog")
    @ApiOperation(value = "admin获取中金历史交易流水",notes = "admin获取中金历史交易流水")
    public Result<Page<PayFundDetailsDTO>> queryPageZjHistoryBalanceLog(PayZjBalanceLogBO bo){
        bo.setUserId(ztgfIncomeUserId);
        return payUserService.queryPageZjHistoryBalanceLog1(bo);
    }

    @PostMapping("/queryPageBalanceList")
    @ApiOperation(value = "admin获取中金交易流水历史列表",notes = "admin获取中金交易流水历史列表")
    public Result<Page<PayFundDetailsDTO>> queryPageBalanceList(PayZjBalanceBO bo){

        return payUserService.queryPageZjHistoryBalanceLogList(bo);
    }
    @PostMapping("/queryPageBalanceListHistory")
    @ApiOperation(value = "admin获取当天中金交易流水列表",notes = "admin获取当天中金交易流水列表")
    public Result<Page<PayZjBalanceLogVO>> queryPageBalanceListHistory(PayZjBalanceLogBO bo){
        bo.setSort(11);
        return payUserService.queryPageZjBalanceLog(bo);
    }
    @PostMapping("/batchExportBalanceList")
    @ApiOperation(value = "导出admin获取中金交易流水历史列表",notes = "导出admin获取中金交易流水历史列表")
    public void batchExportBalanceList(HttpServletResponse response,PayZjBalanceBO bo){
        //创建一个数组用于设置表头
        String[] attributeNames = new String[]{"转账时间", "平台交易流水", "业务类型", "转账金额", "手续费", "转账后余额", "备注", "商户交易流水", "资金账号", "户名","交易对手"};
        List<PayBalanceExpotVO> list= payUserService.batchExportBalanceList(bo);
        if (CustomizeUtil.listIsNull(list)){
            PayBalanceExpotVO vo = new PayBalanceExpotVO();
            BigDecimal price = new BigDecimal("0.00");
            vo.setUseMoney(price.setScale(2, BigDecimal.ROUND_UP));
            vo.setMoeny(price.setScale(2, BigDecimal.ROUND_UP));
            vo.setFeeMoney(price.setScale(2, BigDecimal.ROUND_UP));
            list.add(vo);
        }
            ExcelExport2.export(response, list, attributeNames, "账单");
    }
}
